package com.fub.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.fub.util.PreferenceDAO;
import com.fub.util.Util;

public class LevelChangerView extends View {
	private Paint paintBlock;
	private Paint paintArrow;
	private Paint paintArrowLight;
	private Paint paintTextBold;
	private int textHeight = 20;
	private Context context;
	private int arrowHeight = 20;
	private int width = 60;
	private int padding = 3;
	private int blockHeight = 25;
	private PreferenceDAO preferenceDAO;

	public LevelChangerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		this.preferenceDAO = new PreferenceDAO(context);
		this.paintBlock = new Paint();
		this.paintBlock.setStyle(Style.FILL);
		this.paintBlock.setARGB(200, 45, 65, 82);
		this.paintBlock.setShadowLayer(5, 0, 1, Color.argb(100, 0, 0, 0));

		this.paintArrow = new Paint();
		this.paintArrow.setStyle(Style.FILL_AND_STROKE);
		this.paintArrow.setARGB(200, 0, 0, 0);
		this.paintArrow.setAntiAlias(true);
		this.paintArrow.setShadowLayer(5, 0, 1, Color.argb(100, 0, 0, 0));

		this.paintArrowLight = new Paint();
		this.paintArrowLight.setStyle(Style.FILL);
		this.paintArrowLight.setARGB(100, 120, 120, 120);
		this.paintArrowLight.setAntiAlias(true);

		this.paintTextBold = new Paint();
		this.paintTextBold.setAntiAlias(true);
		this.paintTextBold.setTypeface(Typeface.DEFAULT_BOLD);
		this.paintTextBold.setStyle(Paint.Style.FILL);
		this.paintTextBold.setTextSize(this.textHeight);
		this.paintTextBold.setColor(Color.WHITE);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Path up = new Path();
		up.moveTo(0, this.arrowHeight);
		up.lineTo(this.width / 2, 0);
		up.lineTo(this.width, this.arrowHeight);
		up.lineTo(0, this.arrowHeight);

		Path middle = new Path();
		middle.moveTo(0, this.arrowHeight + this.padding);
		middle.lineTo(this.width, this.arrowHeight + this.padding);
		middle.lineTo(this.width, this.arrowHeight + this.padding
				+ this.blockHeight);
		middle.lineTo(0, this.arrowHeight + this.padding + this.blockHeight);
		middle.lineTo(0, this.arrowHeight + this.padding);

		Path down = new Path();
		down.moveTo(0, this.arrowHeight + 2 * this.padding + this.blockHeight);
		down.lineTo(this.width, this.arrowHeight + 2 * this.padding
				+ this.blockHeight);
		down.lineTo(this.width / 2, 2 * this.arrowHeight + 2 * this.padding
				+ this.blockHeight);
		down.lineTo(0, this.arrowHeight + 2 * this.padding + this.blockHeight);

		int level = new PreferenceDAO(this.context).getCurrentEtage();
		String levelLabel = Util.getFormatedLevel(level);
		float positionLeft = (this.width / 2)
				- Util.getTextWidth(levelLabel, this.paintTextBold) / 2;
		float positonTop = this.arrowHeight + this.padding + this.blockHeight
				- ((this.blockHeight - this.textHeight) / 2 + 2);

		if (Util.levelExist(this.preferenceDAO.getCurrentEtage() + 1,
				this.preferenceDAO.getBuildingId())) {
			canvas.drawPath(up, this.paintArrow);
		} else {
			canvas.drawPath(up, this.paintArrowLight);
		}
		canvas.drawPath(middle, this.paintBlock);
		if (Util.levelExist(this.preferenceDAO.getCurrentEtage() - 1,
				this.preferenceDAO.getBuildingId())) {
			canvas.drawPath(down, this.paintArrow);
		} else {
			canvas.drawPath(down, this.paintArrowLight);
		}
		canvas.drawText(levelLabel, positionLeft, positonTop,
				this.paintTextBold);
		super.onDraw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {

			Rect rectUp = new Rect(0, 0, this.width, this.arrowHeight);
			if (rectUp.contains((int) event.getX(), (int) event.getY())) {
				this.preferenceDAO.setEtage(this.preferenceDAO
						.getCurrentEtage() + 1);
				refreshLayout();
			}
			Rect rectDown = new Rect(0, this.arrowHeight + 2 * this.padding
					+ this.blockHeight, this.width, 2 * this.arrowHeight + 2
					* this.padding + this.blockHeight);
			if (rectDown.contains((int) event.getX(), (int) event.getY())) {
				this.preferenceDAO.setEtage(this.preferenceDAO
						.getCurrentEtage() - 1);
				refreshLayout();
			}
			return true;
		}
		return super.onTouchEvent(event);
	}

	private void refreshLayout() {
		getRootView().invalidate();
	}
}
